package com.appspot.zarm.game;

import java.util.ArrayList;
import java.util.List;

/**
 * This class is the base class for the games. 
 * It is used for the game identification in the container of games.
 * For that purpose all the games ancestors have a pair or more objects having sessionId of all players.
 * To identify game you can use both sessionId.  
 * @author Zhardzetski
 *
 */
public class Game {
	private List<Player> players;

	public Game() {
		players = new ArrayList<Player>();
	}
	
	public boolean hasPlayer(IUser user) {
		for (Player player: players) {
			if (player != null & player.getId().equals(user.getId())) {
				return true;
			}
		}
		return false;
	}

	public void addPlayer(Player player) {
		players.add(player);
	}
	
	public Player getPlayerOne() {
		return players.get(0);
	}
	
	public Player getPlayerTwo() {
		return players.get(1);
	}
	
	/**
	 * Get current player. It may be as playerOne as playerTwo, so select appropriate one using id.
	 * @param user
	 * @return
	 */
	public Player getPlayer(IUser user) {
		for (Player player: players) {
			if (player != null && player.getId().equals(user.getId())) {
				return player;
			}
		}
		return null;
	}
	
	/**
	 * For each player we can find rival using player`s id.
	 * @param user
	 * @return
	 */
	public Player getRival(IUser user) {
		for (Player rival: players) {
			if (rival != null) {
				if (rival.isRobot() || !rival.getId().equals(user.getId())) {
					return rival;
				}
			}
		}
		return null;
	}

}
